package com.guozcc.bootweb.platform.config.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guozcc.bootweb.platform.common.ResultUtils;
import com.guozcc.bootweb.platform.common.TradeErrorCode;
import com.guozcc.bootweb.platform.dto.response.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * API统一异常处理
 */
@ControllerAdvice
public class GlobalApiExceptionHandler {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private HttpServletRequest httpServletRequest;


    /*
     * TradeException正常业务异常处理:200
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(TradeException.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, TradeException ex)  {
        ResultVO<JSONObject> result = ResultUtils.fail(ex.getEc(),ex.getEm());
        this.printResult(result);
        return result;
    }

    /*
     * HttpRequestMethodNotSupportedException  不支持的请求方式:405
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, HttpRequestMethodNotSupportedException ex)  {
        log.error("全局异常处理【"+httpServletRequest.getRequestURI()+"】",ex);
        ResultVO<JSONObject> result = ResultUtils.fail(TradeErrorCode.TRADE_SYSERROR.getErrcode(),"请指定正确的请求Method");
        this.printResult(result);
        return result;
    }

    /*
     * HttpMediaTypeNotSupportedException  不支持的请求Content-type:406
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, HttpMediaTypeNotSupportedException ex)  {
        log.error("全局异常处理【"+httpServletRequest.getRequestURI()+"】",ex);
        ResultVO<JSONObject> result = ResultUtils.fail(TradeErrorCode.TRADE_SYSERROR.getErrcode(),"请指定正确的Content-type");
        this.printResult(result);
        return result;
    }

    /*
     * MultipartException  Current request is not a multipart request 不支持的请求Content-type:406
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MultipartException.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, MultipartException ex)  {
        log.error("全局异常处理【"+httpServletRequest.getRequestURI()+"】",ex);
        ResultVO<JSONObject> result = ResultUtils.fail(TradeErrorCode.TRADE_SYSERROR.getErrcode(),"not a multipart request");
        this.printResult(result);
        return result;
    }

    /*
     * org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestPartException.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, MissingServletRequestPartException ex)  {
        log.error("全局异常处理【"+httpServletRequest.getRequestURI()+"】",ex);
        ResultVO<JSONObject> result = ResultUtils.fail(TradeErrorCode.TRADE_SYSERROR.getErrcode(),"not a multipart request");
        this.printResult(result);
        return result;
    }





    /*
     * Throwable未知系统异常:500
     */
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Throwable.class)
    public ResultVO<JSONObject> globalException(HttpServletResponse response, Exception ex)  {
        log.error("全局异常处理【"+httpServletRequest.getRequestURI()+"】",ex);
        ResultVO<JSONObject> result = ResultUtils.fail(TradeErrorCode.TRADE_SYSERROR.getErrcode(),"未知异常"+ex.getLocalizedMessage());
        this.printResult(result);
        return result;
    }

    /*
     * 打印响应报文
     */
    private void printResult(Object obj){
        try {
            log.info("接口返回报文【{}】", JSON.toJSONString(obj));
        }catch (Exception e){
            log.error("",e);
        }
    }
}
